<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

global $no_menu;
$no_menu = 1;

class MoveRepliesPage {
	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		$checker = new InputChecker();
		check_access("READ");
		if (!has_access("MODERATE") and !has_access("ADMIN"))
			errorpage(5);
	}


	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		$CONTENT = array();
		
		// check access: 
		// 	- check whether user has moderator rights AND 
		//	- check whether supplied ids belong in this directory

		$CONTENT['title'] = "Reacties verplaatsen";

		$sql = db_query_select(array(array(), 'dir_id'), array("(POST INNER JOIN ITEM_IN ON POST.item_id = ITEM_IN.item_id) INNER JOIN SUB_IN ON ITEM_IN.sub_id = SUB_IN.sub_id", "DUMMY"), "post_id IN ({$_GET['ids']}) AND ITEM_IN.item_id = {$page->id}");
		$sqlresult = db_do_query($page, $sql);
		$ok = 0;
		while ($r = db_fetch_array($sqlresult)) {
			if ($r['dir_id'] == $page->directory_tree[$page->dir_depth-1]['dir_id'])
				$ok = 1;
		}
		if (!$ok)
			errorpage(11);
		if (!has_access("MODERATE") and !has_access("ADMIN"))
			errorpage(5);

		if ($_POST['submitted']) {
		
			if ($_POST['dir_id'] and $_POST['sub_id'] and $_POST['item_id']) {
			
				$checker = new InputChecker();
				$ok = 1;
				if ($_POST['item_id'] == "NEW") {
					$ok = ($checker->check_exists($_POST['new_item'], "titel item") and $ok) ? 1 : 0;
					$ok = ($checker->check_tags($_POST['new_item'], "titel item") and $ok) ? 1 : 0;
				}
				if ($_POST['sub_id'] == "NEW") {
					$ok = ($checker->check_exists($_POST['new_subset'], "titel categorie") and $ok) ? 1 : 0;
					$ok = ($checker->check_tags($_POST['new_subset'], "titel categorie") and $ok) ? 1 : 0;
				}
				

				if ($ok) {

					// subset insertion
					if ($_POST['sub_id'] == "NEW") {
					
						$sql = db_query_insert(array('sub_title'), array(db_sqlize($_POST['new_subset'])), "SUBSET");
						db_do_query($page, $sql);
						$sub_id = db_insert_id();
						
						// in_dir insertion
						$sql = db_query_insert(array('dir_id', 'sub_id'), array($_POST['dir_id'], $sub_id), "SUB_IN");
						db_do_query($page, $sql);
						
					} else {
						$sub_id = $_POST['sub_id'];
					}
				
					// item insertion
					if ($_POST['item_id'] == "NEW") {
					
						// fetch title
						$post = db_fetch_array(db_do_query($page, db_query_select(array(array('title')), array('POST'), "post_id IN ({$_GET['ids']})", "creation ASC", "0,1")));
						
						// insert item
						$sql = db_query_insert(array("item_type_id", "author", "editor", "title", "info", "hits", "creation", "edit"), array(1, $_SESSION['user_id'], $_SESSION['user_id'], db_sqlize($post['title']), "''", 0, "NOW(14)", "NOW(14)"), "ITEM");
						db_do_query($page, $sql);
						$item_id = db_insert_id();
						
						// in_sub insertion
						$sql = db_query_insert(array('sub_id', 'item_id'), array($sub_id, $item_id), "ITEM_IN");
						db_do_query($page, $sql);
					
					} else {
						$item_id = $_POST['item_id'];
					}
					
					// move posts
					db_do_query($page, db_query_update(array('creation', 'edit', 'item_id'), array('creation', 'edit', $item_id), "POST", "post_id IN ({$_GET['ids']})"));
					
					// update items post count
					db_do_query($page, db_query_update(array('creation', 'edit', 'num_posts'), array('creation', 'edit', 'num_posts-' . (substr_count($_GET['ids'], ',')+1)), "ITEM", "item_id = {$page->id}"));
					db_do_query($page, db_query_update(array('creation', 'edit', 'num_posts'), array('creation', 'edit', 'num_posts+' . (substr_count($_GET['ids'], ',')+1)), "ITEM", "item_id = {$item_id}"));
			
					endpage("OK, verplaatst" . (($_POST['item_id'] == 'NEW') ? " (pas indien gewenst de titel van het nieuwe topic aan)" : ""));
				}
			}
		}
		
		$inputs = array();
		generate_menu();
		$keys = array();
		$vals = array();
		list($keys, $vals) = generate_list(0, 0, $keys, $vals, $page->menu);
		$creator = new FormCreator();
		$inputs[] = $creator->input_select("Directory", "dir_id", $keys, $vals, "directory", "", "document.moveform.submit()");

		if ($_POST['dir_id']) {
			$sql = db_query_select(array(array("*"), array()), array("SUBSET", "SUB_IN"), "SUBSET.sub_id = SUB_IN.sub_id AND SUB_IN.dir_id = {$_POST['dir_id']}", "sub_title");
			$sqlresult = db_do_query($page, $sql);
			$keys = array();
			$values = array();
			$default = "";
			if (db_num_rows($sqlresult))
				while ($r = db_fetch_array($sqlresult)) {
					$keys[] = $r['sub_id'];
					$title = $r['sub_title'];
					if (strlen($title) > 30)
						$title = substr($title, 0, 30) . "...";
					$values[] = $title;
				}
			$keys[] = "NEW";
			$values[] = $_POST['new_subset'] ? $_POST['new_subset'] : "NIEUWE SET";
			$inputs[] = $creator->input_select("Subset", "sub_id", $keys, $values, "selecteer een categorie", $default, "document.moveform.submit()");
			
			if ($_POST['sub_id'] == "NEW")
				$inputs[] = $creator->input_field("Nieuwe subset", "new_subset");
		}
		
		if ($_POST['sub_id']) {
			if ($_POST['sub_id'] != "NEW") {
				$sql = db_query_select(array(array("*"), array()), array("ITEM", "ITEM_IN"), "ITEM.item_id = ITEM_IN.item_id AND ITEM_IN.sub_id = {$_POST['sub_id']}", "title");
				$sqlresult = db_do_query($page, $sql);
				$keys = array();
				$values = array();
				$default = "";
				if (db_num_rows($sqlresult))
					while ($r = db_fetch_array($sqlresult)) {
						$keys[] = $r['item_id'];
						$title = $r['title'];
						if (strlen($title) > 30)
							$title = substr($title, 0, 30) . "...";
						$values[] = $title;
					}
				$keys[] = "NEW";
				$values[] = $_POST['new_subset'] ? $_POST['new_subset'] : "NIEUW ITEM";
				$inputs[] = $creator->input_select("Item", "item_id", $keys, $values, "selecteer een item", $default, "document.moveform.submit()");
			}
			
			if (($_POST['item_id'] == "NEW") or ($_POST['sub_id'] == "NEW"))
				$inputs[] = $creator->input_field("Nieuw topic", "new_item");
			if ($_POST['sub_id'] == "NEW")
				$inputs[] = $creator->input_hidden("item_id", "NEW");
		}
		
		$CONTENT['form'] = $creator->create("moveform", array_reverse($inputs), $_SERVER['REQUEST_URI']);

		$page->content = $CONTENT;
	}

	function generate_list($parent, $level, $keys, $vals, $menu) {
			
		$rank = 0;	
		$space = "";
		for ($i = 0; $i < $level; $i++)
			$space .= "&nbsp;&nbsp;&nbsp;&nbsp;";
		while ($id = @array_shift($menu[$parent]['children'])) {
			if ($menu[$id]['access'] != "DENY") {
				array_push($keys, "$id");
				array_push($vals, $space . $menu[$id]['menuname']);
				list($keys, $vals) = generate_list($id, ($level+1), $keys, $vals, $menu);
			}
		}	
		return array($keys, $vals);
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		if ($page->content['body'])
			print_body($page);
		if ($page->content['form'])
			print_form($page, $page->content['form']);
	}
}

?>